package router

import (
	"fmt"
	"github.com/valyala/fasthttp"
	"github.com/yeqown/log"
	"net/url"
)

func RenderCtxCue(key string, ctx *fasthttp.RequestCtx) []byte {
	ctxStr := fmt.Sprintf(`%s:{`, key)
	ctx.VisitUserValues(func(bytes []byte, i interface{}) {
		val, err := url.QueryUnescape(fmt.Sprintf("%s", i))
		if err != nil {
			log.Errorf("url.decode:%s error reason:%", i, err)
			return
		}
		valueStr := fmt.Sprintf(`%s:"%s",`, string(bytes), val)
		ctxStr = ctxStr + valueStr
	})
	ctxStr = ctxStr + "}"
	return []byte(ctxStr)
}

func RenderBodyJson(key string, body []byte) []byte {
	str := fmt.Sprintf(`{"%s":%s}`, key, string(body))
	return []byte(str)
}
